package com.leonzx.base.dao;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

/**
 * redis访问类
 * <p>
 * 创建人：梁亮
 * 创建时间：2018-04-09
 */
@Repository
public class RedisDAO {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 存入redis
     *
     * @param key
     * @param val
     */
    public void setKey(String key, String val) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set(key, val);
    }

    /**
     * 存入redis
     *
     * @param key
     * @param val
     * @param timeout  失效时间
     * @param timeUnit 失效时间单位
     */
    public void setKey(String key, String val, long timeout, TimeUnit timeUnit) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set(key, val, timeout, timeUnit);
    }

    /**
     * 获取key的value
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        return ops.get(key);
    }

    /**
     * 模糊获取value
     *
     * @param key
     * @return
     */
    public Set<String> likeValue(String key) {
        Set<String> strs = stringRedisTemplate.keys(key);
        return strs;
    }

    public void remve(String key) {
        stringRedisTemplate.delete(key);
    }
}
